namespace Microsoft.ServiceModel.Samples.Discovery
{
    using System;
    using System.Xml;
    using System.Xml.Serialization;
    using System.ServiceModel;
    using Microsoft.ServiceModel.Samples.Discovery.Protocol;

    public class ServiceProperties
    {
        EndpointAddressAugust2004 endpointReferenceField;
        ContactTypeCollection typesField;
        ScopeCollection scopesField;
        TransportAddressCollection xaddrsField;
        int metadataVersionField;
        XmlElement[] anyField;
        XmlAttribute[] anyAttrField;
        string[] compiledScopes;
        bool compiledScopesSpecified;

        public ServiceProperties()
        {
        }

        public ServiceProperties(EndpointAddress endpointAddress)
            : this(endpointAddress, null, null, 0)
        {
        }

        public ServiceProperties(EndpointAddress endpointAddress, ContactTypeCollection types)
            : this(endpointAddress, types, null, 0)
        {
        }

        public ServiceProperties(EndpointAddress endpointAddress, ScopeCollection scopes)
            : this(endpointAddress, null, scopes, 0)
        {
        }

        public ServiceProperties(EndpointAddress endpointAddress, ContactTypeCollection types,
            ScopeCollection scopes, int metadataVersion)
        {
            EndpointAddress = endpointAddress;
            Types = types;
            Scopes = scopes;
            MetadataVersion = metadataVersion;   
        }

        [XmlElement(
            ElementName = ProtocolStrings.EprElement,
            Namespace = ProtocolStrings.WsaNamespace)]
        public EndpointAddressAugust2004 EndpointAddressAugust2004
        {
            get
            {
                return this.endpointReferenceField;
            }

            set
            {
                this.endpointReferenceField = value;
            }
        }

        [XmlElement]
        public ContactTypeCollection Types
        {
            get
            {
                return this.typesField;
            }
            set
            {
                this.typesField = value;
            }
        }


        [XmlElement]
        public ScopeCollection Scopes
        {
            get
            {
                return this.scopesField;
            }
            set
            {
                this.scopesField = value;
            }
        }


        [XmlElement]
        public TransportAddressCollection XAddrs
        {
            get
            {
                return this.xaddrsField;
            }
            set
            {
                this.xaddrsField = value;
            }
        }

        [XmlElement]
        public int MetadataVersion
        {
            get
            {
                return this.metadataVersionField;
            }
            set
            {
                this.metadataVersionField = value;
            }
        }

        [XmlAnyElement]
        public XmlElement[] AnyElements
        {
            get
            {
                return this.anyField;
            }
            set
            {
                this.anyField = value;
            }
        }

        [XmlAnyAttribute]
        public XmlAttribute[] AnyAttributes
        {
            get
            {
                return this.anyAttrField;
            }
            set
            {
                this.anyAttrField = value;
            }
        }

        [XmlIgnore]
        public EndpointAddress EndpointAddress
        {
            get
            {
                if (this.EndpointAddressAugust2004 != null)
                    return this.EndpointAddressAugust2004.ToEndpointAddress();
                else
                    return null;
            }

            set
            {
                if (value != null)
                {
                    this.EndpointAddressAugust2004 =
                        EndpointAddressAugust2004.FromEndpointAddress(value);
                    this.XAddrs = new TransportAddressCollection();
                    if (value.IsAnonymous)
                        this.XAddrs.Add(new Uri(ProtocolStrings.WSAddressingAugust2004_AnonymousUri));
                    else if (value.IsNone)
                        this.XAddrs.Add(new Uri(ProtocolStrings.WSAddressingAugust2004_NoneUri));
                    else
                        this.XAddrs.Add(value.Uri);
                }
                else
                    this.EndpointAddressAugust2004 = null;
            }
        }

        [XmlIgnore]
        internal string[] CompiledScopes
        {
            get
            {
                return this.compiledScopes;
            }

            set
            {
                this.compiledScopes = value;
            }
        }

        [XmlIgnore]
        internal bool CompiledScopesSpecified
        {
            get
            {
                return this.compiledScopesSpecified;
            }

            set
            {
                this.compiledScopesSpecified = value;
            }
        }

    }
}
